package editbean;

import phonebookcollector.PhoneBookCollector;
import phonebookitem.PhoneBookItem;
import mainbean.MainBean;

public class EditBean extends MainBean{
	private PhoneBookItem a = new PhoneBookItem();
	public String getForm()
	{
		String temp = "";
		PhoneBookItem item = PhoneBookCollector.GetById(Long.valueOf(request.getParameter("id")));
		if(item != null)
		{
			a = item;
			temp+="<form method='post' target='edit.jsp'>" +
				"<p>Name:</p><input type='text' name='name' value='"+item.getName()+"'/>" +
				"<p>Firs tName:</p><input type='text' name='firstname' value='"+item.getFirstName()+"'/>" +
				"<p>Last Name:</p><input type='text' name='lastname' value='"+item.getLastName()+"'/>" +
				"<p>Home Addres:</p><input type='text' name='homeaddress' value='"+item.getHomeAddres()+"'/>" +
				"<p>Email</p><input type='text' name='email' value='"+item.getEmail()+"'/>" +
				"<p>Phone 1</p><input type='text' name='phone1' value='"+item.getPhone1()+"'/>" +
				"<p>Phone 2</p><input type='text' name='phone2' value='"+item.getPhone2()+"'/>" +
				"<p>Phone 3</p><input type='text' name='phone3' value='"+item.getPhone3()+"'/><br/>" +
				"<input type='submit' name='edit' value = 'edit'/>" +
				"</form>";
			return temp;
		}
		return null;
	}
	
	public boolean isEdit()
	{
		if(request.getParameter("edit")!= null)
		{
			PhoneBookItem item = new PhoneBookItem();
			item.setEmail(request.getParameter("email"));
			item.setFirstName(request.getParameter("firstname"));
			item.setHomeAddres(request.getParameter("homeaddress"));
			item.setLastName(request.getParameter("lastname"));
			item.setName(request.getParameter("name"));
			item.setPhone1(request.getParameter("phone1"));
			item.setPhone2(request.getParameter("phone2"));
			item.setPhone3(request.getParameter("phone3"));
			item.setId(Long.valueOf(request.getParameter("id")));
			if(item == a)
			{
				return false;
			}
			PhoneBookCollector.EditItem(item);
			return true;
		}
		return false;
	}
}
